#include <bits/stdc++.h>
#define int long long
using namespace std;
using int128 = __int128_t;
int mod, a, c, x, n, g;

class Matrix {
public:
    int128 n, m, a[3][3];
    Matrix(int N, int M)
    {
        n = N, m = M;
        memset(a, 0, sizeof a);
    }
    Matrix operator*(Matrix& b)
    {
        Matrix c(n, b.m);
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= b.m; j++)
                for (int k = 1; k <= m; k++)
                    c.a[i][j] += a[i][k] * b.a[k][j], c.a[i][j] %= mod;
        return c;
    }
};

Matrix quick_pow(Matrix a, int k)
{
    Matrix ans(a.n, a.n);
    for (int i = 1; i <= a.n; i++)
        ans.a[i][i] = 1;
    while (k) {
        if (k & 1)
            ans = ans * a;
        a = a * a;
        k >>= 1;
    }
    return ans;
}

signed main()
{
    cin >> mod >> a >> c >> x >> n >> g;
    Matrix A(2, 1);
    A.a[1][1] = x, A.a[2][1] = c;
    Matrix b(2, 2);
    b.a[1][1] = a, b.a[1][2] = 1, b.a[2][2] = 1;
    cout << (long long)(((quick_pow(b, n) * A).a[1][1]) % g);
    return 0;
}